/**
 * created by 小卷毛, 2020/7/3
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.zhiyou.aifeng.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.zhiyou.aifeng.common.helper.TimeHelper
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.common_ui.utils.applyFlowableSchedulers
import com.zhiyou.aifeng.model.ResultData
import com.zhiyou.aifeng.model.WalletModel
import io.reactivex.Flowable

/**
 * 项目名称：Smart_Machine
 * 创建人：小卷毛
 * 创建时间：2020-07-03 09:55
 */
class WalletViewModel : DropViewModel() {

    fun getRecordList(
        page: Int,
        userId: String,
        date: String
    ) =
        OkGo.post<BaseResponse<WalletModel>>(BaseUrl.walletRecord)
            .params(
                getParams(
                    "page" to page,
                    "userId" to userId,
                    "userType" to "2",
                    "fundFlow" to "1",
                    "createDate" to date
                )
            )
            .converter(object : JacksonConvert<BaseResponse<WalletModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

    fun getDateList(): Flowable<ArrayList<ResultData>> =
        Flowable.just(2020)
            .map {
                val items = ArrayList<ResultData>()

                val nowDate = TimeHelper.getInstance().getNowTime("yyyy-MM-dd")
                val nowYear = TimeHelper.getInstance().getYearOfDate(nowDate)
                val nowMonth = TimeHelper.getInstance().getMonthOfDate(nowDate)

                for (index in it..nowYear) {
                    val item = ResultData(
                        hint = "${index}年",
                        title = index.toString(),
                        isChecked = index == nowYear
                    )

                    val itemDate = "${index}-01"
                    val innerItems = ArrayList<ResultData>()

                    for(inner in 1..(if (index == nowYear) nowMonth else 12)) {
                        val itemInner = ResultData(
                            hint = "${inner}月份",
                            title = TimeHelper.getInstance().getAfterMonth(itemDate, (inner - 1), "yyyy-MM"),
                            isChecked = item.isChecked && inner == nowMonth
                        )

                        innerItems.add(itemInner)
                    }

                    item.list.addAll(innerItems)
                    items.add(item)
                }

                return@map items
            }
            .compose(applyFlowableSchedulers())

}